from fastapi import APIRouter
from tortoise import Model, fields

app9 = APIRouter()

# 创建models（数据库模型）
class Clas(Model):
    """班级表"""
    id = fields.IntField(pk=True,description="班级ID")
    name = fields.CharField(max_length=50,description="班级名称")
    # room = fields.CharField(max_length=50,description="教室号")

    class Meta:
        table = "class"
        table_description = "班级表"

class Teacher(Model):
    """教师表"""
    # id, 姓名，账号，密码
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=50,description="老师姓名")
    username = fields.CharField(max_length=50,description="老师账户")
    password = fields.CharField(max_length=50,description="密码")

    class Meta:
        table = "teacher"
        table_description = "教师表"


class Course(Model):
    """课程表"""
    # id，课程名，上课老师
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=50,description="课程名称")
    # teacher_id 外键
    teacher = fields.ForeignKeyField(
        'models.Teacher',
        description="授课教师",
        on_delete=fields.CASCADE, # 级联删除
        related_name='course' # 指定从关联模型反向查询时的名称。
    )

    class Meta:
        table = "course"
        table_description = "课程表"

class Student(Model):
    """学生表"""
    # id,姓名，学生账号，密码，学号，班级，所选课程
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=50,description="学生姓名")
    username = fields.CharField(max_length=50,description="学生用户")
    password = fields.CharField(max_length=50,description="密码")
    stuNo = fields.CharField(max_length=50,description="学号")

    # 学生和班级的关系 : 一对一
    clas = fields.ForeignKeyField(
        'models.Clas',
                    related_name='students',
                    on_delete=fields.CASCADE
    )

    # 学生和课程的关系： 一对多
    course = fields.ManyToManyField(
        'models.Course',
        related_name='students',
        on_delete=fields.CASCADE
    )

    class Meta:
        table = "student"
        table_description = "学生表"
